package com.an.nettyl;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;

public class ClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

	@Override
	public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
		System.out.println("---------client------------");
		ctx.channel().writeAndFlush("Hello server ,I am Client!!!".getBytes(CharsetUtil.UTF_8));
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		ctx.channel().writeAndFlush("Hello server ,I am Client!!!".getBytes(CharsetUtil.UTF_8));
	}

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
		// TODO Auto-generated method stub
		try {
			System.out.println("client get :" + msg.toString(CharsetUtil.UTF_8));
			ctx.close();
		} finally {
			ReferenceCountUtil.release(msg);
		}
	}
}
